<?php
namespace app\index\controller;
use think\Session;

class Item extends Ubase
{	
	// 矿机展示列表
	public function index ()
	{
		$user = [];
        $no_title = db('notice')->order('create_time desc')->where('n_keywords',0)->field('n_title')->limit(3)->select();
        $this->assign('notice',$no_title);
        if (Session::has('aid')) {
            $user = db('ns_user')->where('id',Session::get('aid'))->find();
        }
        $this->assign('user',$user);
        $group = db('item_cate')->order('level')->paginate()->each(function($items,$key){
        		$items['data'] = db('candym_type')->where('item_id',$items['id'])->select();
        		return $items;
        });
        $this->assign('group',$group);
		return $this->fetch();
	}

	public function emption ()
	{
		if(is_post()){
			if (Session::has('aid')) {
				$can_id = input('post.can_id');
				$can = db('candym_type')->where('type_id',$can_id)->find();
				if ($can) {
				$candym = [];
					$candym['uid'] = Session::get('aid');
					$candym['type'] = $can['type_id'];
					$candym['candy_total'] = $can['candy_num'];
					$candym['left_days'] = $can['lifetime'];
					return $candym;
				}else{
					return $this->error('请稍后重试');
				}
			} else {
				$this->error('请先登录');
			}
		}else{
			$this->error('您属于非法请求哦');
		}
	}

	public function tofunds()
	{
		if(is_post()) {
			$id = Session::get('aid');
			if($id){
				$post = input('post.');
				if($post['sum'])
				{
					$touser = db('ns_user')->where('id',$id)->find();
					if ($touser['busspwd']) {
						if(encrypt($post['busspwd']) == $touser['busspwd']){
							$candym =  db('candym_type')->where('type_id',$post['type_id'])->find();
							$user = [];
							$can = [];
							if (!($post['cmsn'] % 100) || ($post['cmsn'] == 0) ) {
								if ($touser['commission']>=$post['cmsn']) {
									// 抵扣佣金
									$can['cost'] = ($candym['price']*$post['sum'])-$post['cmsn'];
									if ($touser['available_funds']>=$can['cost']){
										$info['comm'] =$post['cmsn'];
										$user['settlemen_funds'] = $touser['settlemen_funds']+ $can['cost'];
										$user['available_funds'] = $touser['available_funds']-$can['cost'];
										$user['commission'] = $touser['commission'] - $post['cmsn'];
										$can['uid'] = $id;
										$can['type'] = $candym['item_id'];
										$can['candy_total'] = $candym['candy_num'];
										$can['candy_now'] = 0;
										$can['left_days'] = $candym['lifetime'];
										$can['lifetime'] = $candym['lifetime'];
										$can['start'] = date('Y-m-d',time()+24*3600);
										$can['end'] = date('Y-m-d',time()+24*3600*($candym['lifetime']+1));
										$can['create_time'] = date('Y-m-d H:i:s');
										$can['num'] = $candym['num'];
										$can['different'] = $candym['different'];
										$can['phased'] = $candym['phased'];
										$can['counts'] = $post['sum'];
										$can['price'] = $candym['price'];
										$mach = db('candy_machine') -> insert($can);
										$row = db('ns_user')->where('id',$id)->update($user);
										if ($mach&&$row) {
										// // 佣金结算
											$info = [];
											$info['uid'] = $touser['pid'];
											$info['junior_id'] = $id;
											$info['junior_name'] = $touser['username']?$touser['username']:$touser['mobile'];
											$info['account'] = '购买矿机';
											$info['log_time'] = date('Y-m-d H:i:s');
											if($touser['pid']!=0){
												$puser = db('ns_user')->where('id',$touser['pid'])->find();
												$group = db('ns_user_group')->where('id',$puser['group_id'])->find();
												if ( $can['cost'] >= $puser['total_funds'] ) {
													 $puser['commission'] += $info['reward'] =($puser['total_funds']*$group['take_per']/100);
												} else {
													 $puser['commission'] += $info['reward'] = ($can['cost']*$group['take_per']/100);
												}
												db('ns_user')->where('id',$touser['pid'])->update(['commission'=>$puser['commission']]);
												db('charge_log')->insert($info);
											}
											$this -> success('购买成功');
										}else{
											$this->error('请稍后再试');
										}
									}
									$this->error('您的可用资金不足，请充值');
								}
								$this->error('超出您拥有的佣金额度');
							}
							$this->error('请输入100的整数倍');
						}
						$this->error('交易密码错误');
					}
					$this->error('请到个人中心设置交易密码');
				}
				$this->error('请选择购买份额');
			}
			$this->error('请先登录');
		}
	}
}